package cn.zhm.face;



import cn.zhm.face.util.CheckFaceAndEye;
import cn.zhm.face.util.FaceAndEyeToos;

import org.bytedeco.javacpp.opencv_core.Mat;
import org.bytedeco.javacpp.opencv_core.RectVector;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.bytedeco.javacv.OpenCVFrameConverter;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;


/**
 * 检测摄像头是否有人出现
 *
 */
public class CheckFacePoints {
	/**
	 * 从摄像头获取图片
	 * @param frame
	 * @param time
	 */
	static OpenCVFrameConverter.ToMat converter = new OpenCVFrameConverter.ToMat();
	static Java2DFrameConverter java2dFrameConverter = new Java2DFrameConverter();

	// 是否显示眼睛轮廓
	public static boolean SHOW_EYE = true;

	
	public synchronized static Frame dealTheMat(Frame frame){ 
		//图像转换
		Mat mat = f2M(frame); 
		//检测是否有人员
		RectVector faces = CheckFaceAndEye.findFaces(mat);
		//不存在则直接返回
		if(faces == null){
			return m2F(mat);

		}
		// 眼睛
		if (SHOW_EYE){
			FaceAndEyeToos.drawCircleEye(faces, mat);
		}
		FaceAndEyeToos.drawRectangleFace(faces, mat); // 脸框
		return m2F(mat);
	} 
	
	/**
	 * 数据类型转换 Frame to Mat
	 * @param img
	 * @return
	 */
	public static Mat f2M(Frame img){
		return converter.convertToMat(img);
	} 
	
	/**
	 * 数据类型转换 Mat to Frame
	 * @param img
	 * @return
	 */
	public static Frame m2F(Mat img){
		return converter.convert(img);
	}

	/**
	 * 输入图片文件字节和返回的图片类型.
	 * 返回图片字节
	 * @param format
	 * @return
	 * @throws Exception
	 */
	public static byte[] dealTheMat(byte[] s,String format) throws Exception{
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		ByteArrayInputStream in = new ByteArrayInputStream(s);
		BufferedImage read = ImageIO.read(in);
		Frame convert = java2dFrameConverter.convert(read);
		Frame frame = dealTheMat(convert);
		BufferedImage convert1 = java2dFrameConverter.convert(frame);
		ImageIO.write(convert1, format,byteArrayOutputStream);
		return byteArrayOutputStream.toByteArray();
	}

	/**
	 * 输入图片的字节,返回是否存在人脸
	 * @param s
	 * @return
	 */
	public static boolean isFace(byte[] s) {
		try {
			ByteArrayInputStream in = new ByteArrayInputStream(s);
			BufferedImage read = ImageIO.read(in);
			Frame convert = java2dFrameConverter.convert(read);
			Frame frame = dealTheMat(convert);
			Mat mat = f2M(frame);
			in.close();
			RectVector faces = CheckFaceAndEye.findFaces(mat);
			if(faces==null){
				return false;
			}
			return true;
		}catch (Exception e){
			System.out.println("无法识别图片");
			return false;
		}

	}

	/**
	 * 输入图片文件 返回是否存在人脸
	 * @param file
	 * @return
	 */
	public static boolean isFace(File file) {
		try {
			BufferedImage read = ImageIO.read(file);
			Frame convert = java2dFrameConverter.convert(read);
			Frame frame = dealTheMat(convert);
			Mat mat = f2M(frame);
			return CheckFaceAndEye.findFaces(mat) != null;
		}catch (Exception e){
			System.out.println("无法识别图片");
			return false;
		}

	}

	/**
	 * 输入图片文件和返回的图片类型.
	 * 返回图片字节
	 * @param file
	 * @param format
	 * @return
	 * @throws Exception
	 */
	public static byte[] dealTheMat(File file, String format) throws Exception{
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		BufferedImage read = ImageIO.read(file);
		Frame convert = java2dFrameConverter.convert(read);
		Frame frame = dealTheMat(convert);
		BufferedImage convert1 = java2dFrameConverter.convert(frame);
		ImageIO.write(convert1, format,byteArrayOutputStream);
		return byteArrayOutputStream.toByteArray();
	}

}
